package work.vcloud.baseinfo.controller;

import org.springframework.context.annotation.Scope;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import work.vcloud.frame.controller.BaseController;
import work.vcloud.frame.dao.OpenDao;
import work.vcloud.frame.vo.OutputVo;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 *
 * @Version: 1.0
 * @ClassName： MajorController
 * @Description：
 * @Author： EvanZhao
 * @DateTime： 2022-08-25 15:20
 */

@RestController
@Scope("prototype")
public class MajorController extends BaseController {

    @Resource
    OpenDao openDao;

    /**
     * 按年级分组查询专业
     * @return
     */
    @RequestMapping("api/findMajorGroups")
    public OutputVo findMajorGroups() {
        try {
            Map<String, Object> map = getParameterMap();
            map.remove("page");

            List<Map<String, Object>> majorGroups = new ArrayList<>();

            //查询所有专业的年级
            map.put("sqlMapId", "major.findGrade");
            List<Map<String, Object>> gradeList = openDao.queryForList(map);
            for (Map<String, Object> grade: gradeList){
                //查询该年级下设专业
                map.put("fk_grade", grade.get("grade_id").toString());
                map.put("sqlMapId", "major.findMajor");
                List<Map<String, Object>> majorList = openDao.queryForList(map);
                grade.put("major", majorList);
                majorGroups.add(grade);
            }
            return output("0000", "success", majorGroups);
        } catch (Exception e) {
            e.printStackTrace();
            return output("9999");
        }
    }

}
